package com.flute.icrawler.app.util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TextParserImp implements IParser {

	private String content = "";

	public TextParserImp(String content) {
		this.content = content;
	}

	@Override
	public String[] getImgs() throws ParserException {
		HashSet<String> set = new HashSet<String>();

		return set.toArray(new String[0]);
	}

	@Override
	public String[] getLinks() throws ParserException {
		HashSet<String> set = new HashSet<String>();
		// 提前两个引号直接的包含以HTTP开头的内容
		String regEx = "\"(http://.*?[^\"])\"";
		Pattern p = Pattern.compile(regEx);
		Matcher matcher = p.matcher(content);

		while (matcher.find()) {
			set.add(matcher.group(1));
		}

		return set.toArray(new String[0]);
	}

	@Override
	public String getTextContent() {
		return content;
	}

	public static void main(String[] args) {

		byte[] bs = new byte[4 * 1024 * 1024];
		FileInputStream fileInputStream;

		try {
			fileInputStream = new FileInputStream("E://test.html");
			fileInputStream.read(bs);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		String content = new String(bs);

		try {
			String[] strings = new TextParserImp(content).getLinks();
			for (int i = 0; i < strings.length; i++) {
				System.out.println(strings[i]);
			}
		} catch (ParserException e) {
			e.printStackTrace();
		}
	}

}
